
package com.llkj.contractmanagement.main;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.login.LoginActivity;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.ObserverCallBack;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.AppManager;
import com.llkj.contractmanagement.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;


public abstract class BaseFragment extends Fragment implements ObserverCallBack, TitleBar.TitleBarClickListener {
    public Context ctx;
    protected ProgressDialog waitDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentViewLayout(), container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    @Subscribe
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public abstract int getContentViewLayout();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.ctx = context;
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        dismissWaitDialog();
    }

    @Override
    public void onFailureHttp(String message) {
        dismissWaitDialog();
        ToastUtil.makeShortText(ctx, message);
    }

    @Override
    public void onError(Exception error) {
        dismissWaitDialog();
    }

    @Override
    public void onConflict(String message) {
        dismissWaitDialog();
        showConflictDialog(message);
    }

    @Override
    public void onBefore(int resultCode) {
        showWaitDialog();
    }

    @Override
    public void leftClick() {
        getActivity().finish();
    }

    @Override
    public void rightClick() {

    }

    /**
     * 全局等待对话
     */
    public void showWaitDialog() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (waitDialog == null || !waitDialog.isShowing()) {
                    waitDialog = new ProgressDialog(ctx, R.style.MyDialogStyle1);
                    waitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    waitDialog.setCanceledOnTouchOutside(false);
                    View view = LayoutInflater.from(ctx).inflate(
                            R.layout.dialog_waitting, null);
                    waitDialog.show();
                    waitDialog.setContentView(view);
                }
            }
        });

    }

    public void dismissWaitDialog() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (waitDialog != null && waitDialog.isShowing()) {
                        waitDialog.dismiss();
                        waitDialog = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showConflictDialog(String message) {
        new AlertDialog.Builder(ctx).setTitle("提示").setMessage(message).setNegativeButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                UserInfoBean.getInstance().clearUserInfo();
                AppManager.getAppManager().finishAllActivityAndExit();
                Intent intent = new Intent(ctx, LoginActivity.class);
                intent.putExtra("exit", true);
                startActivity(intent);
            }
        }).setCancelable(false).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
